package spring.cloud.tasks.executor.executor.task.execute_statistics;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import spring.cloud.tasks.common.nodepaths.TaskNodePaths;
import spring.cloud.tasks.executor.executor.task.TreeCacheListener;

@Slf4j
@Setter
@Getter
class ExecuteStatisticsResetListener extends TreeCacheListener {


    private final ExecuteStatisticsResetListenerHolder taskResetStatisticsListenerHolder;

    public ExecuteStatisticsResetListener(ExecuteStatisticsResetListenerHolder taskResetStatisticsListenerHolder) {
        this.taskResetStatisticsListenerHolder = taskResetStatisticsListenerHolder;
    }

    @Override
    protected void dataChanged(CuratorFramework curatorFramework, String path, TreeCacheEvent treeCacheEvent) {
        if (taskResetStatisticsListenerHolder.isShutdown) {
            return;
        }
        if (TaskNodePaths.getTaskNodePath(taskResetStatisticsListenerHolder.taskId, ExecuteStatisticsNode.reset_statistics).equals(path) && (TreeCacheEvent.Type.NODE_ADDED == treeCacheEvent.getType() || TreeCacheEvent.Type.NODE_UPDATED == treeCacheEvent.getType())) {
            if (ExecuteStatisticsNode.reset_success_failure_count.equals(new String(treeCacheEvent.getData().getData()))) {
                log.info("清理任务:" + taskResetStatisticsListenerHolder.taskId + "执行统计数据");
                ExecuteStatistics.resetSuccessFailureCount(taskResetStatisticsListenerHolder.taskId);
            }
        }
    }
}
